<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/28
 * Time: 15:01
 */

namespace app\api\service;


class Alipay
{
    const APPID = '2018120562488348';
    const PRIVATE_KEY = 'MIIEpAIBAAKCAQEA7TLBIBJPr1ILsW90fegpBbakxrXONLT5gc0jhvUw6+hfVuoTOjbCj+6940JkkLnJBaZJJfWbGSN1iSWi2hO5pTbHneWkOl+OIylHq8ATCoQippjB8a7Pbu4runlmHd2LYH2ZwuYvHuk84DQdWc3knGuLOcqCgQQIZ3rCj+IIjoE8bLpzGNOc2EE7bNtpdcVCDH44j1AEKj5SBSwTLrJiNDVSYF2slGtJEzSzss1uKFQ4wC6y4U+aho+RpSEOXydxb9SSx3nfW8LsDlsTzGsMa7+X8ZqRo/CBqyAGMIbvzyJaPu86LMyShm9TXK2obED0KQsVFXrldsjmepCZYEqxPQIDAQABAoIBAQCmp4hlXjg6eCzbB/ITHHDmIMN5EffFtHmxp04U3mT2+xcxW9kSh7L/qcAk0lsSDFae74xpbT0B1pYYPCdFjaJb4Dc0S0qt8MFPFwvSMmuucdRetOlfQ3FfAVwb8nu69BOfQeAsvcnRsBIjfLo6hJzhAfaaLhyvRQQaoHfe2/MjG3PqACVY5JLQnzyTvQw3W0axoQ2rF2ueX3kDvTGRIn+puidYxsjazhAa3LA6sA7uRx0zBiYHZQrGPMqWTENI/fgsKM18Bg/8Ak1xB9I7RsN25B6bI62mR19nKFa7ucJulRsk7i6j9/G0Bl6oz5/rVeaZcCW8wT8GSneqzR0M2VKJAoGBAPmYsTiWX33ARlN/DH/elcfnEaNtTZJ1NpziERFhhdwVOlDHeRaUB9NELRo6kT61q37lkNIRL/Jme6m9AKlQHostosyHA68Y3rjuwR6bBPglrLV1l3bAAQJjQL/Tvv7hxkn0QP5vTRtdsuNui/eXN+urIN1pbUI1AVXuWR3SgqZjAoGBAPNIogL2e+3IsvfsmSxN4O4VDtQRPybUsR0eRHa1RzSxaWpCGEVENhPDumGCdpjj9vQKY5AIoEXeL+ZVNqK94Qe89RZRQR2xD1tYgMbRvq9Vocr+UVu/5GQJFTRCg7N1LGs8hLGo4/xkFFe9N1YD8APb2MPFO6lrbJkGUyBGjIvfAoGAB+MqXoZp+BhWYJcYNwY6p3mG1ipDFEi99BE0xt7VExJcqGbVTcBZyvRRlI2bP2cQR09L10eRw1A3wjJlzfPpMy67AUmW9TaM+z0YPzZY3tqZ7ZYMtkT1AzVNu4IefAF4eafNUf0Ea8P3PjalF6rgqoLa5ARL3pa8NxH0IPwhAs8CgYAt2U9nHYrHil5oKPfJCLhORjuwLbvoFod2hGDEum879/903hXy3+MNIIL8o3i0YaoF3ftbhrSl1jTSxNZyHzTMn4jU9uU9skPBpFFIpvi31HyUZA9zKhvh2nBbk/l8FaYUY6hiTwEAbOAElQ1bsYxHPeSBKKeQy0UBJdb+qy0LowKBgQDuWWC9TXN+LzIBaHnLjtcMRYJWY/oF+i+x47pXgZB8EbVQgrUvalOZlnTKzJrGzdrGC8hKSXGjUQihnXknI+TSZcbRCgRvC0kcuXp5bCjRtU0tCWM/5HxZNvWUO/dlDLJLtYzdlP9CWOH+rr3uCnN9eKYxxL6U0aYW29dsZv9cNA==';
    const PUBLIC_KEY = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7TLBIBJPr1ILsW90fegpBbakxrXONLT5gc0jhvUw6+hfVuoTOjbCj+6940JkkLnJBaZJJfWbGSN1iSWi2hO5pTbHneWkOl+OIylHq8ATCoQippjB8a7Pbu4runlmHd2LYH2ZwuYvHuk84DQdWc3knGuLOcqCgQQIZ3rCj+IIjoE8bLpzGNOc2EE7bNtpdcVCDH44j1AEKj5SBSwTLrJiNDVSYF2slGtJEzSzss1uKFQ4wC6y4U+aho+RpSEOXydxb9SSx3nfW8LsDlsTzGsMa7+X8ZqRo/CBqyAGMIbvzyJaPu86LMyShm9TXK2obED0KQsVFXrldsjmepCZYEqxPQIDAQAB';

    public function test1()
    {
        import('alipay.aop.AopClient', EXTEND_PATH, '.php');
        import('alipay.aop.request.AlipayTradeAppPayRequest', EXTEND_PATH, '.php');
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = self::APPID;
        $aop->rsaPrivateKey = self::PRIVATE_KEY;  //'请填写开发者私钥去头去尾去回车，一行字符串';
        $aop->alipayrsaPublicKey = self::PUBLIC_KEY; //'请填写支付宝公钥，一行字符串';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';
        $request = new \AlipayTradeAppPayRequest ();
        /*$request->setBizContent("{" .
            "\"timeout_express\":\"90m\"," .
            "\"total_amount\":\"9.00\"," .
            "\"seller_id\":\"2088102147948060\"," .
            "\"product_code\":\"QUICK_MSECURITY_PAY\"," .
            "\"body\":\"Iphone6 16G\"," .
            "\"subject\":\"大乐透\"," .
            "\"out_trade_no\":\"70501111111S001111119\"," .
            "\"time_expire\":\"2016-12-31 10:05\"," .
            "\"goods_type\":\"0\"," .
            "\"promo_params\":\"{\\\"storeIdType\\\":\\\"1\\\"}\"," .
            "\"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," .
            "\"royalty_info\":{" .
            "\"royalty_type\":\"ROYALTY\"," .
            "        \"royalty_detail_infos\":[{" .
            "          \"serial_no\":1," .
            "\"trans_in_type\":\"userId\"," .
            "\"batch_no\":\"123\"," .
            "\"out_relation_id\":\"20131124001\"," .
            "\"trans_out_type\":\"userId\"," .
            "\"trans_out\":\"2088101126765726\"," .
            "\"trans_in\":\"2088101126708402\"," .
            "\"amount\":0.1," .
            "\"desc\":\"分账测试1\"," .
            "\"amount_percentage\":\"100\"" .
            "          }]" .
            "    }," .
            "\"extend_params\":{" .
            "\"sys_service_provider_id\":\"2088511833207846\"," .
            "\"hb_fq_num\":\"3\"," .
            "\"hb_fq_seller_percent\":\"100\"," .
            "\"industry_reflux_info\":\"{\\\\\\\"scene_code\\\\\\\":\\\\\\\"metro_tradeorder\\\\\\\",\\\\\\\"channel\\\\\\\":\\\\\\\"xxxx\\\\\\\",\\\\\\\"scene_data\\\\\\\":{\\\\\\\"asset_name\\\\\\\":\\\\\\\"ALIPAY\\\\\\\"}}\"," .
            "\"card_type\":\"S0JP0000\"" .
            "    }," .
            "\"sub_merchant\":{" .
            "\"merchant_id\":\"19023454\"," .
            "\"merchant_type\":\"alipay: 支付宝分配的间连商户编号, merchant: 商户端的间连商户编号\"" .
            "    }," .
            "\"enable_pay_channels\":\"pcredit,moneyFund,debitCardExpress\"," .
            "\"store_id\":\"NJ_001\"," .
            "\"specified_channel\":\"pcredit\"," .
            "\"disable_pay_channels\":\"pcredit,moneyFund,debitCardExpress\"," .
            "\"settle_info\":{" .
            "        \"settle_detail_infos\":[{" .
            "          \"trans_in_type\":\"cardSerialNo\"," .
            "\"trans_in\":\"A0001\"," .
            "\"summary_dimension\":\"A0001\"," .
            "\"amount\":0.1" .
            "          }]" .
            "    }," .
            "\"invoice_info\":{" .
            "\"key_info\":{" .
            "\"is_support_invoice\":true," .
            "\"invoice_merchant_name\":\"ABC|003\"," .
            "\"tax_num\":\"1464888883494\"" .
            "      }," .
            "\"details\":\"[{\\\"code\\\":\\\"100294400\\\",\\\"name\\\":\\\"服饰\\\",\\\"num\\\":\\\"2\\\",\\\"sumPrice\\\":\\\"200.00\\\",\\\"taxRate\\\":\\\"6%\\\"}]\"" .
            "    }," .
            "\"ext_user_info\":{" .
            "\"name\":\"李明\"," .
            "\"mobile\":\"16587658765\"," .
            "\"cert_type\":\"IDENTITY_CARD\"," .
            "\"cert_no\":\"362334768769238881\"," .
            "\"min_age\":\"18\"," .
            "\"fix_buyer\":\"F\"," .
            "\"need_check_info\":\"F\"" .
            "    }," .
            "\"business_params\":\"{\\\"data\\\":\\\"123\\\"}\"" .
            "  }");*/
        $bizcontent = "{\"body\":\"我是测试数据\","
            . "\"subject\": \"App支付测试\","
            . "\"out_trade_no\": \"20170125test01\","
            . "\"timeout_express\": \"30m\","
            . "\"total_amount\": \"0.01\","
            . "\"product_code\":\"QUICK_MSECURITY_PAY\""
            . "}";
        $request->setBizContent($bizcontent);
        $result = $aop->sdkExecute($request);
        dump($result);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if (!empty($resultCode) && $resultCode == 10000) {
            echo "成功";
        } else {
            echo "失败";
        }
    }

    public static function appPay($info)
    {
        import('alipay.aop.AopClient', EXTEND_PATH, '.php');
        import('alipay.aop.request.AlipayTradeAppPayRequest', EXTEND_PATH, '.php');
        $aop = new \AopClient;
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = self::APPID;
        $aop->rsaPrivateKey = self::PRIVATE_KEY;
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = self::PUBLIC_KEY;
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new \AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数，这里只需要传入业务参数
        $content = [
            'body'=>$info['title'],
            'subject'=>$info['title'],
            'out_trade_no'=>$info['order_no'],
            'timeout_express'=>'30m',
//            'total_amount'=>0.01,
            'total_amount'=>$info['amount'],
            'product_code'=>'QUICK_MSECURITY_PAY'
        ];
        $bizcontent = json_encode($content);
        $request->setNotifyUrl('http://'."{$_SERVER['SERVER_NAME']}".'/api/callback/alipayApppayNotify');
        $request->setBizContent($bizcontent);
//这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
//        $response = substr($response,35);
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
//        return htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
        return $response;//就是orderString 可以直接给客户端请求，无需再做处理。
    }

    //提现
    public static function alipayTransfer($info){
        import('alipay.aop.AopClient', EXTEND_PATH, '.php');
        import('alipay.aop.request.AlipayFundTransToaccountTransferRequest', EXTEND_PATH, '.php');
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018120562488352';
        $aop->rsaPrivateKey = 'MIIEpAIBAAKCAQEAqbf67nVoUH1fcEpkWuriQlGSknKPiir9wv0e4e2hq04ulMfS05qNiP4exjCfJWtF+DSKVN7qCWEbODHy4h3kAQdwyIian8UZMsKdHBOCAuctxuPecBEZqApov5EbAeSlLXg+59LRNCPDNnvi8IqfI02/zkLBER5HcDPIaXEhQ+emVx7si8UKFv2E0fp/5oWbirTsJu9F4EhZzyooCXD9Ee991z0u5cRAAZHgKwff7uTKfrkiC1fl8/sXLFXP4wysAGdJa7BEtLVakZhXiQOg+XDzP7ct+NoFlfTjyq1SjXLaj6ifOvcJkUDqOX57wHhtJlLKkoVPBJ8ffup5+V0V3wIDAQABAoIBAA0pytmR25xajPttmaj/jLruDrszDXT+7p2f+jQ1tRNPRgeeE/j7hLy6UPK4NK+3ST79/Dim7Wn60Ryjqx3kiZmW1SWIis2XgOPR+lOCI9RLz94cMFVqzh2daRBqlbio3jLPn7llPXqYx3xDUY+sgLlBP34k6UqFma7XJV UgRB87CPNFIxWrzeqXcQc8/cckonX8+zKgK+QXNO6lOty4TZ1VLDy/dQb3xNk8FLWDMqntFIXwukNRk+1Bk66OwBxhMmdLrlXluisJntyl+IIjCQ+6CWigZIGfNoeFOHibCxUtykqYETX5CqmA9vz1moNlC85Wn14gfjGAiYVHK06snRkCgYEA0RlFK2/aXWnvza6oY8rcnhAJYJFYHNDY9oGOlj/0TyOwQ1CPOPFtj9sT2Qh6xsRfnhHBcsCvolHbYpqH5Zltbn0b346ugYy1QLU9iDQI91hPpoWLh4glwY+YiCejHs7a2hkZUaR+Wp45o5ptE6gBqCAUbwg0IHaBgFNva8aOFMUCgYEAz8l0gUHYqj0Tk6XDLRLtJwxmsDf+4lYKW01/sVFCiuVK/C30IQtqY7+AZdb1pR8M1Nd5g0VtbT4GeeOF9jegaNn3ebLmQqOnkPU+Mf1Rh8v8H9HNHD2mNODz9DKzyxO3GmGXHou7LqUVdCd5YmeOu5ZDTtx3vpinxNn1AerkklMCgYEAm7OmhxbOUtsMv2s8ZrrrlyO359my9BosRTrqE0XiRX/8w9NcIvncLM/ksqsWyaX20XzFQQ7segFcTMtnQHgEPc8Zj1nu5fcCDQIopKCiwJRZE1XENZMTElQ8L77cP5em7drcuKe1kwLZRtM6P/XN7gSdUqSYNEgqXIVEpct/h0UCgYAlaLif+NHgPGTE5UgRWmH18MagHUS3TRRKQdamfuHDDesFIeTc+igqUzTzoNLS6pctzqli1+IOk+colKzumi84EV37vBi2kK4/a9fJujiugSwgMm6CEhtVU9a39VTnT+JcToudhTyq9i+5nM2+yLQq06/Eh+7LEsLJrw/sS3ThXwKBgQCNITksPaa9ybmqPqEJGqjGYUJpnV7RHsXN23HT+wr400x3kW1m3rqAJl3ICWQgG2JN7eCyDId5LIBIYTeHAnTxUxWSDmhzcSv7wkWWhnGnMBP33t2jEIWWCa3tzqM/WqrhhvEypdwVCtHUZYf1X+zv58IlIKF+BC/iyrad9RNB+w==';
        $aop->alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkVAUqI/vkhAGVmqLACQrBiVTQwHrUve/WrLn8O2b9q/xphnMCz+k99itNrBkjtZUBZ4ARuVoqLoOeo5EK6bYMes4uajXe9DRUhsWLTpTKNjAmrIV/2iLyedefR5YQUwnynfbqkdWW1eW+YjyKL5bMSoAX2ZZAgeW1XAkAyWvCKd5hG2ATLMkhfQLmA80LMvIGiPZWpCfpZCHJUlc2vV+KYsiazCE5TzAFE6j7btTKInzAbWcqma9N8EM4+WsAjN82sfPZJ40v0KNWalKZJmpYAQTqKjHLZFA9DntjE4eqnEaVI3Vx0ajQz6sBarsb6ilXBtNY5nE3lEMxFIdtZBpGQIDAQAB';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';
        $request = new \AlipayFundTransToaccountTransferRequest ();
        $request->setBizContent("{" .
            "\"out_biz_no\":\"{$info['order_no']}\"," .
            "\"payee_type\":\"ALIPAY_LOGONID\"," .
            "\"payee_account\":\"{$info['alipay']}\"," .
            "\"amount\":\"{$info['actual_amount']}\"," .
            "\"payer_show_name\":\"余额提现\"," .
            "\"payee_real_name\":\"{$info['realname']}\"," .
            "\"remark\":\"余额提现\"" .
            "  }");
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
        /*$resultCode = $result->$responseNode->code;
        if (!empty($resultCode) && $resultCode == 10000) {
            return $result->$responseNode;
        } else {
            return false;
        }*/
    }

}